﻿#pragma once
#include<assert.h>
#include<iostream>//c++的输入输出流
using std::cout, std::endl;
#include<vector>
using std::vector;
#include<list>
using std::list;
using std::swap;
namespace myspace
{
	/*less仿函数*/
	template<class T>
	struct less
	{
		bool operator()(const T& x1, const T& x2)
		{
			return x1 < x2;
		}
	};
	/*priority_queue模版函数*/
	template<class T,class Container=vector<T>,class Compare=less<typename Container::value_type>>
	class priority_queue
	{
	public:
		
		void adjust_up(size_t ch)
		{
			Compare com;
			size_t child = ch;
			size_t father = (child - 1) / 2;
			while (child>0&&/*_con[father] < _con[child]*/com(_con[father],_con[child]))
			{
				swap(_con[father], _con[child]);
				child=father;
				father = (child - 1) / 2;
			}
		}
		void push(const T& x)
		{
			_con.push_back(x);
			adjust_up(_con.size() - 1);
		}
		void adjust_down(int root)
		{
			Compare com;
			size_t prt = root;
			size_t child = prt * 2 + 1;
			while (child < _con.size())
			{
				if (child + 1 < _con.size() &&com(_con[child] , _con[child + 1]))++child;
				if (com(_con[child],_con[prt]))break; 
				
				swap(_con[child], _con[prt]);
				prt = child;
				child = prt * 2 + 1;
			}
		}
		void pop()
		{
			swap(_con[0], _con[_con.size()-1]);
			_con.pop_back();
			adjust_down(0);
		}
		T& top()
		{
			return _con[0];
		}
		size_t size()
		{
			return _con.size();
		}
		bool empty()
		{
			return _con.empty();
		}
		
	private:
		Container _con;
	};

	
	/*___________________*/

	

	

	void test_priority_queue1()
	{
		cout << "test_priority_queue1" << endl;
		priority_queue<int>pq;
	
		pq.push(2);
		pq.push(1);
		pq.push(9);
		pq.push(8);
		pq.push(3);
		pq.push(7);
		pq.push(2);
		
		while(!pq.empty())
		{
			cout << pq.top() << " ";
			pq.pop();
		}
		cout << endl;

		cout << "test_priority_queue1" << endl;
	}
	void test_priority_queue2()
	{
		cout << "test_priority_queue2" << endl;
		priority_queue<int, vector<int>, greater<int>>pq;

		pq.push(2);
		pq.push(1);
		pq.push(9);
		pq.push(8);
		pq.push(3);
		pq.push(7);
		pq.push(2);

		while (!pq.empty())
		{
			cout << pq.top() << " ";
			pq.pop();
		}
		cout << endl;
		cout << "test_priority_queue2" << endl;
	}
	
}